package com.zhupf.hash;

import java.util.*;

/**
 * @author zhupf
 * @date 2023年10月24日 10:34
 * @Description
 */
public class Train2 {
    public static void main(String[] args) {
        String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
        List<List<String>> test = test(strs);
        System.out.println(test.toString());
    }

    public static List<List<String>> test(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();
        for (String str : strs) {
            char[] charArray = str.toCharArray();
            Arrays.sort(charArray);
            String newStr = new String(charArray);
            if(map.containsKey(newStr)){
                map.get(newStr).add(str);
            }else {
                List<String> list = new ArrayList<>();
                list.add(str);
                map.put(newStr,list);
            }
        }
        List<List<String>> values = new ArrayList<>(map.values());
        return values;
    }

}
